package com._520it.crm.web.controller;

import com._520it.crm.domain.OrderBill;
import com._520it.crm.domain.Reviseinfo;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.service.IReviseinfoService;
import com._520it.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/reviseinfo")
public class ReviseinfoController {
    @Autowired
    private IReviseinfoService reviseinfoService;

    @RequiresPermissions("reviseinfo:index")
    @PermissionName("批改页面")
    @RequestMapping("")
    public String index(){
        //System.out.println("http://localhost/reviseinfo");
        return "reviseinfo_index";
    }

    @RequiresPermissions("reviseinfo:tempindex")
    @PermissionName("批改暂存页面")
    @RequestMapping("/temp")
    public String tempindex(){
        return "reviseinfotemp_index";
    }

    @RequiresPermissions("reviseinfo:revise")
    @PermissionName("保单批改")
    @RequestMapping("/revise")
    public String revise(Long orderBillId,Long reasonValue, Model model){
        //把保单id 和 修改原因 带到批改页面
        model.addAttribute("orderBillId",orderBillId);
        model.addAttribute("reasonValue",reasonValue);
        return "reviseinfo";
    }

    @RequiresPermissions("reviseinfo:save")
    @PermissionName("批改保存申请核保")
    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(Reviseinfo info,Long orderBillId,Long status){
        AjaxResult result = null;
        info.setId(null);
        info.setOrderBillId(orderBillId);
        try{
            if(status == null){
                reviseinfoService.insert(info,-1,-1);
            }else{
                //申请核保: service 保存的同时中记录 temp 为 0, 并修改 保单的 状态为 7 批改待审核
                reviseinfoService.insert(info,Reviseinfo.TEMP_NO, OrderBill.REVISETOUNDERWRITING);
            }
            result = new AjaxResult(true,"保存成功");
        }catch(Exception e){
            e.printStackTrace();
            result = new AjaxResult("保存失败,请联系管理员!");
        }
        return result;
    }

    @RequestMapping("/selectReviseinfo")
    @ResponseBody
    public  Map<String,Object> selectReviseinfo(Long orderBillId){
        Map<String, Object> info = null;
        info = reviseinfoService.selectReviseinfo(orderBillId);
        return info;
    }

}
